<?php


use yii\helpers\Html;
use yii\grid\GridView;
use yii\bootstrap\ActiveForm;
use backend\assets\AppAsset;
use kartik\date\DatePicker;


/* @var $this yii\web\View */
/* @var $searchModel backend\models\PlanDataSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '合同列表';
$this->params['breadcrumbs'][] = $this->title;

$complete = \backend\models\PlanData::find()->orderBy('id DESC')->select('complete')->scalar();


?>
<div class="plan-data-index">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-solid">
                <div class="box-header with-border">
                    <p>
                        <a class="btn btn-success" href="javascript:;" onclick="add_contract()">添加合同</a>
                        <a class="btn btn-info" href="index">实时数据</a>
                    </p>
                </div>
                <div class="box-body">
                    <?= GridView::widget([
                        'dataProvider' => $dataProvider,
                        'filterModel' => $searchModel,
                        'columns' => [
                            ['class' => 'yii\grid\SerialColumn'],
                            [
                                'attribute' => 'type',
                                'value' =>
                                    function ($model) {
                                        return dropDown('type', $model->type);
                                    },
                                'filter' => dropDown('type'),
                            ],
                            'contract_name',
                            'complete',
                            'contract',
                            [
                                'label' => '添加时间',
                                'attribute' => 'created_at',
                                'value' => function ($model) {
                                    return date('Y-m-d H:i:s', $model->created_at);
                                },
                                'filter' => DatePicker::widget([    // 日期组件
                                    'model' => $searchModel,
                                    'language' => Yii::$app->language,
                                    'attribute' => 'created_at',
                                    'value' => $searchModel->created_at,
                                    'options' => ['readonly' => true],
                                    'pluginOptions' => [
                                        'autoclose' => true,
                                        'format' => 'yyyy-mm-dd',
                                        'todayHighlight' => true,
                                    ]
                                ])
                            ],
                            [
                                'class' => 'yii\grid\ActionColumn',
                                'header' => '操作',
                                'template' => '{show}&nbsp;&nbsp;{update}',
                                'headerOptions' => ['width' => '10%'],
                                'buttons' => [
                                    'update' => function ($url, $model, $key) {
                                        $max_id = \backend\models\PlanData::find()->orderBy('id DESC')->select('id')->scalar();
                                        if ($model->id == $max_id) {
                                            $url = 'javascript:;';
                                            return Html::button('编辑', [
                                                'contract_id' => $model->id,
                                                'complete' => $model->complete,
                                                'contract' => $model->contract,
                                                'contract_type' => $model->type,
                                                'contract_name' => $model->contract_name,
                                                'contract_img' => $model->contract_img,
                                                'class' => 'btn btn-info',
                                                'onclick' => 'edit_contract($(this))',
                                            ]);
                                        }
                                    },
                                    'show' => function ($url, $model, $key) {
                                        $url = 'javascript:;';
                                        return Html::button('查看合同', [
                                            'contract_id' => $model->id,
                                            'class' => 'btn btn-success',
                                            'onclick' => 'show_contract($(this))',
                                        ]);

                                    },
                                ],
                            ],
                        ],
                    ]); ?>
                </div>
            </div>

        </div>
    </div>
</div>

<div class="modal fade" tabindex="-1" role="dialog" id="detailModal1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"></div>
            <div class="modal-body">
                <div class="options-box">
                    <div class="options-box-body">
                        <p class="options-box-msg text-center"><i class="fa fa-pencil"></i> 编辑合同</p>
                        <?php $form = ActiveForm::begin(['id' => 'plan-data-form', 'action' => 'update']); ?>

                        <?= $form->field($model = new \backend\models\PlanData(), 'id')->label(false)->hiddenInput() ?>

                        <?= $form->field($model, 'complete')->textInput(['readonly' => 'readonly']) ?>

                        <?= $form->field($model, 'type')->label('合同类型')->dropDownList([1 => '软件合同', 2 => '硬件合同', 3 => '其他合同'], ['prompt' => '请选择']) ?>

                        <?= $form->field($model, 'contract_name')->label('合同名称')->textInput() ?>

                        <?= $form->field($model, 'contract')->label('合同金额')->textInput() ?>

                        <label>现有合同图片</label>
                        <img id="plandata-contract_img" style="width: 50px;height: 50px;"/>

                        <?= $form->field($model = new \common\models\UploadForm(), 'imageFile')->label('合同图片')->fileInput() ?>

                        <div class="row">
                            <div class="col-xs-12">
                                <button class="btn btn-primary btn-block btn-flat" type="submit">保存</button>
                            </div>
                            <!-- /.col -->
                        </div>
                        <?php ActiveForm::end(); ?>
                    </div>
                    <!-- /.change-password-box-body -->
                </div>

            </div>
            <div class="modal-footer">
                <button id="close_modal" type="button" class="btn btn-default btn-sm" data-dismiss="modal">关闭
                </button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" tabindex="-1" role="dialog" id="detailModal2">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"></div>
            <div class="modal-body">
                <div class="options-box">
                    <div class="options-box-body">
                        <p class="options-box-msg text-center"><i class="fa fa-pencil"></i> 添加新合同额</p>
                        <?php $form = ActiveForm::begin(['id' => 'plan-data-form', 'action' => 'create']); ?>

                        <div class="form-group field-plandata-complete">
                            <label class="control-label" for="plandata-complete">已完成金额</label>
                            <input id="plandata-complete" class="form-control" name="PlanData[complete]"
                                   readonly type="text" value="<?= $complete ?>">

                            <p class="help-block help-block-error"></p>
                        </div>

                        <?= $form->field($model = new \backend\models\PlanData(), 'type')->label('合同类型')->dropDownList([1 => '软件合同', 2 => '硬件合同', 3 => '其他合同'], ['prompt' => '请选择']) ?>

                        <?= $form->field($model, 'contract_name')->label('合同名称')->textInput() ?>

                        <?= $form->field($model, 'contract')->label('合同金额')->textInput() ?>

                        <?= $form->field($model = new \common\models\UploadForm(), 'imageFile')->label('合同图片')->fileInput() ?>

                        <div class="row">
                            <div class="col-xs-12">
                                <button class="btn btn-primary btn-block btn-flat" type="submit">提交</button>
                            </div>
                            <!-- /.col -->
                        </div>
                        <?php ActiveForm::end(); ?>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button id="close_modal" type="button" class="btn btn-default btn-sm" data-dismiss="modal">关闭
                </button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" tabindex="-1" role="dialog" id="detailModal3">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"></div>
            <div class="modal-body">
                <div class="options-box">
                    <div class="options-box-body">
                        <p class="options-box-msg text-center">合同图片</p>
                        <img id="contrac_img_show" style="width: 100%"/>
                    </div>
                    <!-- /.change-password-box-body -->
                </div>

            </div>
            <div class="modal-footer">
                <button id="close_modal" type="button" class="btn btn-default btn-sm" data-dismiss="modal">关闭
                </button>
            </div>
        </div>
    </div>
</div>
<script>
    function add_contract() {
        $('#detailModal2').modal();
    }

    function edit_contract(obj) {
        var contract_id = obj.attr('contract_id');
        var complete = obj.attr('complete');
        var contract = obj.attr('contract');
        var contract_type = obj.attr('contract_type');
        var contract_name = obj.attr('contract_name');
        var contract_img = obj.attr('contract_img');

        $('#plandata-complete').val(complete);
        $('#plandata-id').val(contract_id);
        $('#plandata-contract').val(contract);
        $('#plandata-type').val(contract_type);
        $('#plandata-contract_img').attr('src', '/uploads' + contract_img);
        $('#plandata-contract_name').val(contract_name);

        $('#detailModal1').modal();
    }

    function contract_update() {

    }

    //查看合同
    function show_contract(obj) {
        var contract_id = obj.attr('contract_id');
        var url = 'show';
        var data = {contract_id: contract_id};
        $.ajax({
            async: false,
            cache: false,
            type: 'POST',
            url: url,// 请求的action路径
            //dataType: 'json',
            data: data,
            error: function () {// 请求失败处理函数

            },
            success: function (result) {
                $('#contrac_img_show').attr('src', '/uploads' + result);
            },
        });

        $('#detailModal3').modal();
    }
</script>
